/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BLL;

import DAL.MyConnectUnit;
import DTO.Users;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.HashSet;

/**
 *
 * @author Quan_Nuce
 */
public class BUsers {
    MyConnectUnit connect;
    public BUsers() {
        this.connect = DAL.DAL.getDAL();
}
      public HashSet<Users>getUserss(String condition, String OderBy) throws Exception{
        ResultSet result = this.connect.Select("Users",condition,OderBy);
        HashSet<Users> userss = new HashSet<Users>();
        while(result.next()){
            Users users = new Users();
            users.setUserID(result.getInt("UserID"));
            users.setFullName(result.getString("FullName"));
            users.setAddress(result.getString("Address"));
            users.setPhone(result.getInt("Phone"));
            users.setEmail(result.getString("Email"));
            users.setSex(result.getString("Sex"));
            users.setDepartmentID(result.getInt("DepartmentID"));
            users.setRoleID(result.getInt("RoleID"));
            
            
            userss.add(users);
        }
        return userss;
    }
        
  public HashSet<Users>getUserss(String condition) throws Exception{
        return getUserss(condition,null);
    }
  public HashSet<Users>getUserss() throws Exception{
        return getUserss(null);
    }
   
   public Users getByID(int id)throws Exception{
        HashSet<Users>userss = this.getUserss("UserID = "+ id);
        if(userss.size()>0)
            return userss.toArray(new Users[userss.size()])[0];
        return null;
       
   }
     public void Insert(Users users)throws Exception{
        HashMap<String, Object>map = new HashMap<String,Object>();
        map.put("UserID", users.getUserID());
         map.put("FullName", users.getFullName());
          map.put("Address", users.getAddress());
           map.put("Phone", users.getPhone());
            map.put("Email", users.getEmail());
             map.put("Sex", users.getSex());
              map.put("DepartmentID", users.getDepartmentID());
               map.put("RoleID", users.getRoleID());
                
       
        this.connect.Insert("Users", map);
       
   }
         public void Inserts(HashSet<Users> userss)throws Exception{
        for(Users users:userss)
            this.Insert(users);
       
}
          public void Update(Users users)throws Exception{
        HashMap<String, Object>map = new HashMap<String,Object>();
        map.put("FullName", users.getFullName());
          map.put("Address", users.getAddress());
           map.put("Phone", users.getPhone());
            map.put("Email", users.getEmail());
             map.put("Sex", users.getSex());
              map.put("DepartmentID", users.getDepartmentID());
               map.put("RoleID", users.getRoleID());
        this.connect.Update("Users", map,"UserID="+users.getUserID());
       
   }
             public void Updates(HashSet<Users> userss)throws Exception{
        for(Users users:userss)
            this.Update(users);
       
   }
   public void Delete(Users users) throws Exception{
        this.connect.Delete("Users", "UserID="+users.getUserID());
    }
       public void Deletes(HashSet<Users> userss) throws Exception {
        for(Users users:userss)
            this.Delete(users);
   }
}